﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_ICM_BidOfferCollateral : IDAL_ICM_BidOfferCollateral
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();
        

        public D_ICM_BidOfferCollateral()
        {

        }

        public IList<M_ICM_BidOfferCollateral> GetFull(Nullable<int> icm_bid_seq, Nullable<int> icm_col_type_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ICM_BidOfferCollateral> infoList = new List<M_ICM_BidOfferCollateral>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@icm_bid_seq", icm_bid_seq);
                dbManager.AddParameters(2, "@icm_col_type_seq", icm_col_type_seq);

                strSQL = "SELECT offc.icm_bid_off_col_seq, offc.icm_col_type_seq, offc.icm_bid_off_col_ref_no"
                    + ", offc.icm_bid_off_col_amount, offc.icm_bid_off_col_descr, offc.icm_bid_off_seq"
                    + ", offc.receipt_no"
                    + " FROM icm_bid_offer_collateral offc"
                    + " LEFT JOIN icm_bid_offer off ON offc.icm_bid_off_seq = off.icm_bid_off_seq"
                    + " WHERE off.icm_bid_seq = @icm_bid_seq"
                    + " AND offc.icm_col_type_seq = @icm_col_type_seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ICM_BidOfferCollateral info = new M_ICM_BidOfferCollateral();
                        info.icm_bid_off_col_seq = Convert.ToInt32(drc[i]["icm_bid_off_col_seq"]);
                        info.icm_col_type_seq = cn.ConvertNullInt(drc[i]["icm_col_type_seq"]);
                        info.icm_bid_off_col_ref_no = drc[i]["icm_bid_off_col_ref_no"].ToString();
                        info.icm_bid_off_col_amount = cn.ConvertNullDouble(drc[i]["icm_bid_off_col_amount"]);
                        info.icm_bid_off_col_descr = drc[i]["icm_bid_off_col_descr"].ToString();
                        info.icm_bid_off_seq = cn.ConvertNullInt(drc[i]["icm_bid_off_seq"]);
                        info.receipt_no = drc[i]["receipt_no"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_ICM_BidOfferCollateral> GetList(Nullable<int> icm_bid_seq, Nullable<int> icm_col_type_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ICM_BidOfferCollateral> infoList = new List<M_ICM_BidOfferCollateral>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@icm_bid_seq", icm_bid_seq);
                dbManager.AddParameters(2, "@icm_col_type_seq", icm_col_type_seq);

                strSQL = "SELECT offc.icm_bid_off_col_seq, offc.receipt_no"
                    + " FROM icm_bid_offer_collateral offc"
                    + " LEFT JOIN icm_bid_offer off ON offc.icm_bid_off_seq = off.icm_bid_off_seq"
                    + " WHERE off.icm_bid_seq = @icm_bid_seq"
                    + " AND offc.icm_col_type_seq = @icm_col_type_seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ICM_BidOfferCollateral info = new M_ICM_BidOfferCollateral();
                        info.icm_bid_off_col_seq = Convert.ToInt32(drc[i]["icm_bid_off_col_seq"]);
                        info.receipt_no = drc[i]["receipt_no"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
         
        public M_ICM_BidOfferCollateral GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_ICM_BidOfferCollateral info = new M_ICM_BidOfferCollateral();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT offc.BidderColId, offc.ColTypeId, offc.Code"
                    + ", offc.Amount, offc.Descr, offc.BidderId, bidder.BidId, bid.Name as bid_name"
                    + ", offc.ReceiptNo, bidder.ConId, con.NameTh as con_name"
                    + " FROM IcmBidderCollateral offc"
                    + " LEFT JOIN IcmBidder bidder ON offc.BidderId = bidder.BidderId"
                    + " LEFT JOIN IcmBid bid ON bidder.BidId = bid.BidId"
                    + " LEFT JOIN IcmMasContact con ON bidder.ConId = con.ConId"
                    + " WHERE offc.BidderColId = @seq" ;

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.icm_bid_off_col_seq = Convert.ToInt32(drc[0]["BidderColId"]);
                    info.icm_col_type_seq = cn.ConvertNullInt(drc[0]["ColTypeId"]);
                    info.icm_bid_off_col_ref_no = drc[0]["Code"].ToString();
                    info.icm_bid_off_col_amount = cn.ConvertNullDouble(drc[0]["Amount"]);
                    info.icm_bid_off_col_descr = drc[0]["Descr"].ToString();
                    info.icm_bid_off_seq = cn.ConvertNullInt(drc[0]["BidderId"]);
                    info.receipt_no = drc[0]["ReceiptNo"].ToString();

                    info.icm_bid_seq = cn.ConvertNullInt(drc[0]["BidId"]);
                    info.icm_bid_name = drc[0]["bid_name"].ToString();
                    info.icm_con_seq = cn.ConvertNullInt(drc[0]["ConId"]);
                    info.icm_con_name = drc[0]["con_name"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }


        public List<M_ICM_BidOfferCollateral> GetInfoBidderCollateral(int poId)
        {
            string strSQL;
            DataRowCollection drc = null;
            var infos = new List<M_ICM_BidOfferCollateral>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@poId", poId);

                strSQL = @"SELECT offc.BidderColId, offc.ColTypeId, offc.Code
	, offc.Amount, offc.Descr, offc.BidderId, bidder.BidId, bid.Name as bid_name
	, offc.ReceiptNo, bidder.ConId, con.NameTh as con_name
FROM IcmPurchaseOrder po 
	LEFT JOIN IcmBidder bidder ON po.BidderId = bidder.BidderId
	INNER JOIN IcmBidderCollateral offc on offc.BidderId = bidder.bidderid
	LEFT JOIN IcmBid bid ON bidder.BidId = bid.BidId
	LEFT JOIN IcmMasContact con ON bidder.ConId = con.ConId
WHERE po.poid = @poId" ;

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    var info = new M_ICM_BidOfferCollateral();
                    info.icm_bid_off_col_seq = Convert.ToInt32(drc[0]["BidderColId"]);
                    info.icm_col_type_seq = cn.ConvertNullInt(drc[0]["ColTypeId"]);
                    info.icm_bid_off_col_ref_no = drc[0]["Code"].ToString();
                    info.icm_bid_off_col_amount = cn.ConvertNullDouble(drc[0]["Amount"]);
                    info.icm_bid_off_col_descr = drc[0]["Descr"].ToString();
                    info.icm_bid_off_seq = cn.ConvertNullInt(drc[0]["BidderId"]);
                    info.receipt_no = drc[0]["ReceiptNo"].ToString();

                    info.icm_bid_seq = cn.ConvertNullInt(drc[0]["BidId"]);
                    info.icm_bid_name = drc[0]["bid_name"].ToString();
                    info.icm_con_seq = cn.ConvertNullInt(drc[0]["ConId"]);
                    info.icm_con_name = drc[0]["con_name"].ToString();
                    infos.Add(info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infos;
        }

        //public string SelectRecentInsertedRoleSeq()
        //{
        //    string strSQL;
        //    string strResult;

        //    try
        //    {
        //        dbManager.Open();

        //        strSQL = "select scope_identity() as last_seq";

        //        strResult = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows[0]["last_seq"].ToString();
        //    }
        //    catch
        //    {
        //        throw;
        //    }
        //    finally
        //    {
        //        dbManager.Dispose();
        //    }

        //    return strResult;
        //}

        public string Insert(M_ICM_BidOfferCollateral info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@icm_bid_off_col_seq", info.icm_bid_off_col_seq);
                dbManager.AddParameters(1, "@icm_col_type_seq", info.icm_col_type_seq);
                dbManager.AddParameters(2, "@icm_bid_off_col_ref_no", info.icm_bid_off_col_ref_no);
                dbManager.AddParameters(3, "@icm_bid_off_col_amount", info.icm_bid_off_col_amount);
                dbManager.AddParameters(4, "@icm_bid_off_col_descr", info.icm_bid_off_col_descr);
                dbManager.AddParameters(5, "@icm_bid_off_seq", info.icm_bid_off_seq);
                dbManager.AddParameters(6, "@receipt_no", info.receipt_no);

                dbManager.AddParameters(7, "@created_by", info.created_by);
                dbManager.AddParameters(8, "@created_when", info.created_when);

                strSQL = "INSERT INTO icm_bid_offer_collateral"
                    + " (icm_col_type_seq, icm_bid_off_col_ref_no, icm_bid_off_col_amount"
                    + ", icm_bid_off_col_descr, icm_bid_off_seq, receipt_no"
                    + ", created_by, created_when)"
                    + " VALUES(@icm_col_type_seq"
                    + ", @icm_bid_off_col_ref_no"
                    + ", @icm_bid_off_col_amount"
                    + ", @icm_bid_off_col_descr"
                    + ", @icm_bid_off_seq"
                    + ", @receipt_no"

                    + ", @created_by"
                    + ", @created_when"
                    + " );"
                    + " select scope_identity();";
                var strSeq = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows[0][0].ToString();
                return strSeq;
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_ICM_BidOfferCollateral info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@icm_bid_off_col_seq", info.icm_bid_off_col_seq);
                dbManager.AddParameters(1, "@icm_col_type_seq", info.icm_col_type_seq);
                dbManager.AddParameters(2, "@icm_bid_off_col_ref_no", info.icm_bid_off_col_ref_no);
                dbManager.AddParameters(3, "@icm_bid_off_col_amount", info.icm_bid_off_col_amount);
                dbManager.AddParameters(4, "@icm_bid_off_col_descr", info.icm_bid_off_col_descr);
                dbManager.AddParameters(5, "@icm_bid_off_seq", info.icm_bid_off_seq);
                dbManager.AddParameters(6, "@receipt_no", info.receipt_no);

                dbManager.AddParameters(7, "@updated_by", info.updated_by);
                dbManager.AddParameters(8, "@updated_when", info.updated_when);

                strSQL = "UPDATE IcmBidderCollateral"
                    + " set ColTypeId = @icm_col_type_seq"
                    + ", Code = @icm_bid_off_col_ref_no"
                    + ", Amount = @icm_bid_off_col_amount"
                    + ", Descr = @icm_bid_off_col_descr"
                    + ", BidderId = @icm_bid_off_seq"
                    + ", ReceiptNo = @receipt_no"

                    + ", UpdatedBy = @updated_by"
                    + ", UpdatedWhen = @updated_when"
                    + " WHERE BidderColId = @icm_bid_off_col_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int seq)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "DELETE FROM icm_bid_offer_collateral"
                    + " WHERE icm_bid_off_col_seq = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
